import { BeLEDRGB } from '../../../../model/parts/BeLED'
import { AppModel } from '../../../../model/AppModel'
import { createPartMenu } from '../BlockPackage'

export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model:AppModel) {
        this.appendDummyInput()
            .appendField("RGB LED")
            .appendField(createPartMenu(model, BeLEDRGB), 'part')
        this.appendValueInput("color")

    } ,

    run (args: any, util: any, runtime: any) {
    } ,

    coder: {
        JavaScript(block: any, coder: any, model: AppModel) {
            
            // 
            let partUUID = block.getFieldValue('part')
            let part = model.findPartFromUUID(partUUID)
            if(!part)
                return "// unknow RGB LED part uuid: " + partUUID

            let val = coder.valueToCode(block, 'color', coder.ORDER_ASSIGNMENT) || '0'

            return `${coder.indentString()}var _colorValue = ${val}\n`
                + `${coder.indentString()}be.part["${part.instanceName}"].setValue(_colorValue[0],_colorValue[1],_colorValue[2])\n`
        }
    }
}
